<?php

include_once "network.php";

/**
 * SHA1 class
 *
 * 计算公众平台的消息签名接口.
 */
class SHA1
{
	/**
	 * 用SHA1算法生成安全签名
	 * @param string $token 票据
	 * @param string $timestamp 时间戳
	 * @param string $nonce 随机字符串
	 * @param string $encrypt 密文消息
	 */
	public function getSHA1($token, $timestamp, $nonce, $encrypt_msg)
	{
		//排序
		try {
			$array = array($encrypt_msg, $token, $timestamp, $nonce);
			sort($array, SORT_STRING);
			$str = implode($array);
			return array(sha1($str));
		} catch (Exception $e) {
			//print $e . "\n";
			return array(null);
		}
	}

}


//验证
function checkLink(){
	// 获取微信服务器访问本服务器发送过来的内容，他都是通过get请求发送的

	$signature = $_GET['signature'];
	$timestamp = $_GET['timestamp'];
	$nonce = $_GET['nonce'];
	$echostr = $_GET['echostr'];

	//设置token
	$token = 'Tiankong';

	//实例化加密验证算法
	$sha = new SHA1();

	$arr = $sha->getSHA1($token,$timestamp,$nonce,null);

	if($arr[0]==$signature){
		return true;
	}else{
		return false;
	}
}
//执行验证
if(isset($_GET['echostr'])){
	if(checkLink()){
		echo $_GET['echostr'];
	}
}







?>